//
//  ALPS_TProvinceViewController.m
//  Alps
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TProvinceViewController.h"
#import "ALPS_TPRadioModel.h"
#import "ALPS_TComprehensiveTCell.h"
@interface ALPS_TProvinceViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *lelfTableView;
@property (nonatomic, strong) NSMutableArray *leftData;
@property (nonatomic, strong) UITableView *rightTableView;
@property (nonatomic, strong) NSMutableArray *rightData;
//加载页
@property (nonatomic, assign) NSInteger page;
//判断请求是不是点击的请求
@property (nonatomic, assign) NSInteger temp;
//保存广播ID
@property (nonatomic, assign) NSInteger staging;
@end

@implementation ALPS_TProvinceViewController

- (void)dealloc
{
    _lelfTableView.delegate = nil;
    _lelfTableView.dataSource = nil;
    _rightTableView.dataSource = nil;
    _rightTableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftData = [NSMutableArray array];
    self.rightData = [NSMutableArray array];
    self.page = 1;
    [self getLelfView];
    [self getRightView];
    [self getData];
    [self getRightData:1];
    [self setLeftBackButton];
}

//左边界面
-(void)getLelfView
{
    self.lelfTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth * 0.23, ScreeHeight - 64) style:UITableViewStylePlain];
    self.lelfTableView.delegate = self;
    self.lelfTableView.dataSource = self;
    [self.view addSubview:_lelfTableView];
    [self.lelfTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

//左边界面请求数据
-(void)getData
{
    [WSSNetTool GetNetByURL:@"http://live.ximalaya.com/live-web/v1/getProvinceList" body:nil headers:nil response:0 success:^(id result) {
        NSArray *array = [result objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            ALPS_TPRadioModel *model = [[ALPS_TPRadioModel alloc] initWithDic:dic];
            [self.leftData addObject:model];
        }
        [self.lelfTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

//右边界面请求数据
-(void)getRightData:(NSInteger)page
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&pageSize=20&provinceCode=110000&radioType=2", page] body:nil headers:nil response:0 success:^(id result) {
        NSArray *array = [result objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            ALPS_TPRadioModel *model = [[ALPS_TPRadioModel alloc] initWithDic:dic];
            [self.rightData addObject:model];
        }
        [self.rightTableView.mj_header endRefreshing];
        [self.rightTableView.mj_footer endRefreshing];
        [self.rightTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

//点击左边界面请求的数据
-(void)didClickderData:(NSInteger)num page:(NSInteger)page
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&pageSize=20&provinceCode=%ld&radioType=2", page, num] body:nil headers:nil response:0 success:^(id result) {
        NSArray *array = [result objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            ALPS_TPRadioModel *model = [[ALPS_TPRadioModel alloc] initWithDic:dic];
            [self.rightData addObject:model];
        }
        [self.rightTableView.mj_header endRefreshing];
        [self.rightTableView.mj_footer endRefreshing];

        [self.rightTableView reloadData];
    } failure:^(NSError *error) {
        
}];




}


//右边界面
-(void)getRightView
{
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(ScreeWidth * 0.23, 0, ScreeWidth - ScreeWidth * 0.23, ScreeHeight - 64) style:UITableViewStylePlain];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.separatorStyle = UITableViewCellSelectionStyleGray;
    [self.view addSubview:_rightTableView];
    [self.rightTableView registerClass:[ALPS_TComprehensiveTCell class] forCellReuseIdentifier:@"ALPS_TComprehensiveTCell"];
    
    self.rightTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    [self refreshData];
   }];

    self.rightTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    [self loadingData];
   }];

}


-(void)refreshData
{
    [self.rightData removeAllObjects];
    [self.rightTableView reloadData];
    if (self.temp == 1) {
        [self didClickderData:self.staging page:1];
    }
    else
    {
    [self getRightData:1];
    }
    self.page = 1;
}
-(void)loadingData
{
    if (self.temp == 1) {
        self.page++;
        [self didClickderData:self.staging page:self.page];
    }else
    {
    self.page++;
    [self getRightData:self.page];
    }
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_TPRadioModel *model = [self.leftData objectAtIndex:indexPath.row];
    if (tableView == self.lelfTableView) {
        [self.rightData removeAllObjects];
        [self.rightTableView reloadData];
        self.staging = model.provinceCode;
        [self didClickderData:model.provinceCode page:1];
        self.temp = 1;
    }
    else
    {
        ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc]init];
        ALPS_TCWRadioModel *model = [self.rightData objectAtIndex:indexPath.row];
        NSDictionary *dic = model.radioPlayUrl;
        pmVC.radioName = model.rname;
        pmVC.radioURL = [dic objectForKey:@"radio_64_aac"];
        pmVC.isRedioYesOrNo = YES;
        pmVC.pictureURL = model.radioCoverLarge;
        [self.navigationController pushViewController:pmVC animated:YES];

    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.rightTableView) {
        return 100;
    }
    return 44;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.lelfTableView) {
        return self.leftData.count;
    }
    else
    {
        return self.rightData.count;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.lelfTableView) {
        
        ALPS_TPRadioModel *model = [self.leftData objectAtIndex:indexPath.row];
        if (tableView == self.lelfTableView) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = model.provinceName;
            return cell;
        }
    }
    else
    {
        ALPS_TComprehensiveTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TComprehensiveTCell"];
        
        cell.modelP = [self.rightData objectAtIndex:indexPath.row];
        return cell;
    }
    
    
    return nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
